package com.lyz.myself.service.ServiceImpl;

import com.lyz.myself.mapper.FileMapper;
import com.lyz.myself.mapper.RFileLeadsMapper;
import com.lyz.myself.pojo.Po.FileLeads;
import com.lyz.myself.service.FileService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @program: myself
 * @Date: 2019/2/14 11:18
 * @Author: Mr.Deng
 * @Description:
 */
@Service
public class FileServiceImpl implements FileService {

    @Autowired
    FileMapper fileMapper;

    @Autowired
    RFileLeadsMapper rFileLeadsMapper;

    /**
     * 添加附件
     */
    @Override
    public int insert(List<FileLeads> list){
        return fileMapper.insert(list);
    }

    /**
     *根据线索id查询附件
     * @mbg.generated 2019-02-14
     */
    @Override
    public List<Map<String,String>> selectByleadsId(Integer leads_id, Integer company_id){
        return fileMapper.selectByleadsId(leads_id,company_id);
    }


    /**
     *删除附件（可批量）
     * @mbg.generated 2019-02-14
     */
    @Override
    public int deleteFile(List<Integer> list){
         return fileMapper.deleteFile(list);
    }

    /**
     *根据id批量修改线索附件
     */
    @Override
    public int updateFiled(Map<String,Object> map){
       return fileMapper.updateFiled(map);
    }

    /**
     *根据路径查询附件id
     */
    @Override
    public String findFiledId(String filePath){
        return fileMapper.findFiledId(filePath);
    }

    /**
     * 删除线索附件
     * @param file_path
     * @return
     */
    @Transactional
    @Override
    public boolean deleteLeadsFile(String file_path){
        boolean boo;
        String file_id = fileMapper.findFiledId(file_path);
        if(file_id!=null){
            int deleteFileLeads = rFileLeadsMapper.deleteLeadsFile(Integer.parseInt(file_id));
            int deleteFile = fileMapper.deleteLeadsFile(file_path);
            if(file_id!=null&&deleteFileLeads>0&&deleteFile>0){
                boo = true;
            }else{
                boo = false;
            }
        }else{
            boo = false;
        }
        return boo;
    }
}
